﻿@model IEnumerable<BuildServer.Models.ScheduleViewModel>

@{
    ViewBag.Title = "Schedule Builds";
}

<h2 class="title">
    <img src="~/Images/BuildSchedule.png" />@ViewBag.Title
</h2>

@using (Html.BeginForm("Schedule", "Schedule"))
{
    @Html.AntiForgeryToken()

    <table class="data-table">
        <thead>
            <tr>
                <th class="data-group"></th>
                <th colspan="2">@Html.DisplayNameFor(m => m.Definition.Name)</th>
                <th width="100">Last Run</th>
                <th width="70">@Html.DisplayNameFor(m => m.Definition.IsNightly)</th>
            </tr>
        </thead>
        @foreach (var group in Model.GroupBy(x => x.Template.Name).OrderBy(x => x.Key))
        {
            @GroupHeader(group.Key, 4)
            @GroupContent(group);
        }
    </table>
    <h3 style="margin-bottom: 2px">Build Target</h3>
    <div style="border: 1px solid #ddd; padding: 5px; display: inline-block;">
        <table style="border-spacing: 0; margin-bottom: 5px;">
            <tr>
                @BuildTarget("0", "None", "Unschedule")
                @BuildTarget("1", "Build", "Build", true)
                @BuildTarget("2", "Clean;Build", "Clean & Build")
                @BuildTarget("3", "Publish", "Build & Publish")
                @BuildTarget("4", "Clean;Publish", "Clean, Build & Publish")
            </tr>
            <tr>
                @BuildTargetDescription("0", "Don't build anything")
                @BuildTargetDescription("1", "Compile all projects")
                @BuildTargetDescription("2", "Remove existing files and <strong>Build</strong>")
                @BuildTargetDescription("3", "<strong>Build</strong> and publish installer packages")
                @BuildTargetDescription("4", "Remove existing files and <strong>Build & Publish</strong>")
            </tr>
        </table>
    </div>
    <p>
        <input type="submit" value="Schedule" />
    </p>
}

@helper BuildTarget(string id, string target, string label, bool isChecked = false)
{
    string checkedValue = isChecked ? "checked" : null;
    <td>
        <input id="target_@id" style="cursor:pointer;" name="targets" type="radio" checked="@checkedValue" value="@target" />
    </td>
    <td>
        <label for="target_@id" style="cursor:pointer;font-weight:bold;">@label</label>
    </td>
}

@helper BuildTargetDescription(string id, string description)
{
    <td></td>
    <td style="font-size: .85em; padding-right: 2em;">
        <label for="target_@id" style="cursor:pointer">@Html.Raw(description)</label>
    </td>
}

@helper GroupHeader(string groupName, int columns)
{
    groupName = string.IsNullOrWhiteSpace(groupName) ? "[other]" : groupName;
    <tbody class="data-group-header">
        <tr>
            <td colspan="@columns">
                <span class="data-group-expand"><img src="~/Images/CollapsedFilePicker_14x_F12.png" width="10" /> @groupName</span>
                <span class="data-group-collapse"><img src="~/Images/ExpandedFilePicker_14x_F12.png" width="10" /> @groupName</span>
            </td>
        </tr>
    </tbody>
}

@helper GroupContent(IEnumerable<BuildServer.Models.ScheduleViewModel> items)
{
    <tbody class="data-group-content">
        @foreach (var item in items)
        {
            string checkboxId = "buildDefinitions_" + item.Definition.Id;
            <tr class="data-row">
                <td class="data-group"></td>
                <td class="data-check">
                    <input style="cursor:pointer;" type="checkbox" id="@checkboxId" name="buildDefinitions" value="@item.Definition.Id" />
                </td>
                <td>
                    <label style="cursor:pointer;" for="@checkboxId">@Html.DisplayFor(modelItem => item.Definition.Name)</label>
                </td>
                <td>
                    @if(item.Run != null && item.Run.End.HasValue)
                    {
                        <div style="font-size:0.9em;line-height:0.9em;">
                            @item.Run.End.Value.ToShortDateString()<br />@item.Run.End.Value.ToShortTimeString()
                        </div>                        
                    }
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Definition.IsNightly, "YesNo")
                </td>
            </tr>
        }
    </tbody>
}

@section Scripts
{
    <script type="text/javascript">
        function toggle_data_group() {
            var groupHeader = $(this).closest('.data-group-header')
            groupHeader.find('.data-group-expand, .data-group-collapse').toggle();
            groupHeader.next('.data-group-content').toggle();
        }
        $(document).ready(function () {
            $('.data-group-expand').show().click(toggle_data_group);
            $('.data-group-collapse').hide().click(toggle_data_group);
            $('.data-group-content').hide();
            $('.data-group-expand:first').click();
        });
    </script>
}
